package com.hyj.jpdy.grap.html;



import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.hyj.jpdy.action.LoginAction;
import com.hyj.jpdy.bean.vo.Article;
import com.hyj.jpdy.grap.html.bean.HtmlInfoVO;
import com.hyj.jpdy.grap.html.bean.HtmlQO;



public class JokeGrap {
	static Log log = LogFactory.getLog(JokeGrap.class);
	
	public static List<Article> grapNewJoke(Document doc,String baseUrl){
		List<Article> list = new ArrayList<Article>();
		
		 // 最新笑话
		 Elements newList = doc.getElementsByAttributeValue("class", "newcontent l_left").get(0)
		 .getElementsByTag("li");
		for(Element e:newList){
			Article item = new Article();
			Element a = e.getElementsByTag("a").get(0);
			String href = a.attr("href");
			String title = a.text();
			
			String pubDate = e.getElementsByTag("span").text();
			log.debug("标题："+title+"   发布日期："+pubDate+"   地址:"+baseUrl+href);
			item.setTitle(title);
			item.setPubDate(pubDate);
			item.setFromUrl(baseUrl+href);
			item.setColumnName("笑话集");
			String contentId ="text110";
			
			
			HtmlQO qo = new HtmlQO();
			qo.setCharset("GBK");
			qo.setUrl(baseUrl+href);
			qo.setContentId(contentId);
			
			HtmlInfoVO vo =	GrapHtmlUtil.getContent(qo);
			if(vo !=null){
				List<String> clist = vo.getContent();
				for(String str:clist){
					log.info(str);
				}
				item.setContent(vo.getTextContent());
				list.add(item);
			}
		}
		
		
		return list;
	}
	
	/**
	 * @param args
	 * @throws UnsupportedEncodingException 
	 */
	public static void main(String[] args) {
		 String url = "http://www.jokeji.cn/";
		 Document doc = GrapHtmlUtil.getDocument(url,"GBK");
		 if(doc !=null){
			 // 最新笑话
			 Elements newList = doc.getElementsByAttributeValue("class", "newcontent l_left").get(0)
			 .getElementsByTag("li");
			for(Element e:newList){
				Element a = e.getElementsByTag("a").get(0);
				String href = a.attr("href");
				String title = a.text();
				
				String pubDate = e.getElementsByTag("span").text();
				System.out.println("标题："+title+"   发布日期："+pubDate+"   地址:"+url+href);
				
				String contentId ="text110";
				
				
				HtmlQO qo = new HtmlQO();
				qo.setCharset("GBK");
				qo.setUrl(url+href);
				qo.setContentId(contentId);
				
				HtmlInfoVO vo =	GrapHtmlUtil.getHtmlContent(qo);
				List<String> list = vo.getContent();
				for(String str:list){
				  System.out.println(str);
				}
				System.out.println("==========================================");
			}
			
			//  笑话分类
			Elements typeList = doc.getElementsByAttributeValue("class", "joketype l_left").get(0)
			.getElementsByTag("a");
			for(Element e:typeList){
				String href = e.attr("href");
				String type = e.text();
				System.out.println("----------------->>类型："+type+"   地址："+url+href);
				grapType(url,url+href);
			}
			 
		 }
	}
	
	/**
	 * 分类抓取
	 * @param url
	 */
	public static List<Article> grapType(String baseUrl,String url){
		List<Article> alist = new ArrayList<Article>();
		 Document doc = GrapHtmlUtil.getDocument(url,"GBK");
		 Elements list = doc.getElementsByAttributeValue("class", "list_title");
		 list  = (list!=null&&list.size()>0) ? list.get(0).getElementsByTag("li") : null;
		 
		if (list != null) {
			for (Element e : list) {
				Article item = new Article();
				Element a = e.getElementsByTag("a").get(0);
				String title = a != null ? a.text() : "";
				String href = a != null ? a.attr("href") : "";

				Element span = e.getElementsByTag("i").get(0);
				String pubDate = span != null ? span.text() : "";

				log.info("articl--文章标题：" + title + "   发布日期：" + pubDate+ "   地址:" + baseUrl+href);
				item.setTitle(title);
				item.setPubDate(pubDate);
				item.setFromUrl(baseUrl+href);
				item.setColumnName("笑话集");
				
				String contentId ="text110";
				HtmlQO qo = new HtmlQO();
				qo.setCharset("GBK");
				qo.setUrl(baseUrl+href);
				qo.setContentId(contentId);
				
				HtmlInfoVO vo =	GrapHtmlUtil.getContent(qo);
				if(vo !=null){
					List<String> cList = vo.getContent();
					for(String str:cList){
						log.debug(str);
					}
					item.setContent(vo.getTextContent());
				}
				alist.add(item);
			}
		}else{
			log.info("分类无法抓取"+url);
		}
		 
		return alist;
	}
	

}